<?php

namespace App\Services;


use App\Models\User as UserModel;
use App\Models\DrawImg;
use App\Common\GenerateCartoonizedImageAdvance;
use App\Common\GetAsyncJobResult;

class PetService extends UserModel
{
    /**提交生图
     * @param $param
     * @return void
     */
    public function drawImg($param)
    {
        $user = getUser();
        $requestId = GenerateCartoonizedImageAdvance::main(['img' => $param['img'], 'type' => $param['type']]);
        if (!$requestId) {
            throwError('暂不能生成');
        }
        $drawImgModel = new DrawImg();
        $drawImgModel->uid = $user->id;
        $drawImgModel->img = $param['img'];
        $drawImgModel->request_id = $requestId;
        $drawImgModel->type = $param['type'];
        $drawImgModel->save();
        SetReturnJson([]);
    }

    /**生图历史
     * @return void
     */
    public function drawImgList()
    {
        $user = getUser();
        $noEndlist = DrawImg::where(['uid' => $user->id, 'status' => 0])->get();
        if ($noEndlist) {
            foreach ($noEndlist as &$value) {
                $draw_img = GetAsyncJobResult::main(['jobId' => $value->request_id]);
                if ($draw_img) {
                    $value->draw_img = $draw_img;
                    $value->status = 1;
                    $value->save();
                }
            }
        }
        $list = DrawImg::where(['uid' => $user->id])->get();
        SetReturnJson($list);
    }
}
